const app = getApp()
const Api = require('../../../utils/api.js')

Page({
   data: {
      contentLength: 0,
      maxLength: 200,
      content: '',
   },

   formData: {
      content: '',
      cover: '',
   },

   // 输入事件 
   handleContentChange(e) {
      this.setData({
         contentLength: e.detail.value.length
      })
      this.formData.content = e.detail.value
   },

   handleSubmit() {
      const uploader = this.selectComponent('#uploader')
      this.formData.cover = uploader.data.image.urls.join(',')
      console.log(this.formData)
      if (this.formData.content.trim().length < 5) {
         wx.showToast({ title: '多写点内容吧', icon: 'none' })
         return
      }
      if (this.formData.content.length > 200) {
         wx.showToast({ title: '内容有点多哦', icon: 'none' })
         return
      }
      wx.showLoading({ title: '发布中' })
      app.post(Api.dynamic.create, {
         ...this.formData,
      }).then(res => {
         wx.hideLoading()
         if (res.code === 1){
            wx.showToast({ title: '发布成功' })
            wx.navigatorBack({})
            this.formData = {
               content: '', cover: ''
            }
            this.setData({
               content: ''
            })
            uploader.imageClear()
         }
            
		}).catch(err => {
         wx.hideLoading()
      })
   }
})